﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.service;
using shop.entity;
using shop.exception;
using shop.util;
using shop.handler;

namespace shop.handler
{
    /// <summary>
    /// GoodsHandler 的摘要说明
    /// </summary>
    public class GoodsHandler : IHttpHandler
    {
        private GoodsService goodsService = new GoodsService();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string method = context.Request["method"];

            if (method == null || method.Trim().Equals(""))
            {
                //toDo
                printErrorJson(context, "method not exist");
                return;
            }
            method = method.Trim();
            if (method.Equals("add"))
            {
                Add(context);
            }
            else if (method.Equals("update"))
            {
                Update(context);
            }
            else if (method.Equals("delete"))
            {
                Delete(context);
            }
            else
            {
                printErrorJson(context, "method not error");
            }
        }

        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            double price = Double.Parse(context.Request["price"]);
            string description = context.Request["description"];
            int catagoryId = int.Parse(context.Request["catagoryId"]);
            int imageId = int.Parse(context.Request["imageId"]);
            Image image = new Image()
            {
                Id = imageId
            };

            Goods goods = new Goods()
            {
                Name = name,
                Price = price,
                Description = description,
                CategoryId = catagoryId,
                Image = image

            };
            string json;
            try
            {
                goodsService.AddGoods(goods);

                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("添加商品成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            context.Response.Write(json);
        }
        private void Update(HttpContext context)
        {

            int id = RequestUtil.ParseStringToInt(context.Request, "id");
            string name = context.Request["name"];
            double price = Double.Parse(context.Request["price"]);
            string description = context.Request["description"];
            Goods goods = new Goods()
            {
                Name = name,
                Price = price,
                Description = description,
                Id = id
            };


            string json;
            try
            {
                goodsService.UpdateGoods(goods);

                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("更新商品成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            context.Response.Write(json);
        }

        private void Delete(HttpContext context)
        {
            int id = RequestUtil.ParseStringToInt(context.Request, "id");

            string json;
            bool result = false;
            try
            {
                result = goodsService.DeleteGoods(id);

                json = JsonUtil.getJsonString(JsonResult<string>.RenderSuccessMessage("删除品成功！"));
            }
            catch (ShopParamException e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.ErrorMessage));
            }
            catch (Exception e)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(1, e.Message));
            }
            if (!result)
            {
                json = JsonUtil.getJsonString(JsonResult<string>.RenderError(2, "删除商品失败，商品id错误"));
               
            }
            context.Response.Write(json);
        }
        private void printErrorJson(HttpContext context, string error)
        {
            context.Response.Write(JsonUtil.getJsonString(JsonResult<string>.RenderError(1, error)));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }
}